import { DataSet } from 'choerodon-ui/pro';
import {
  organizationsApiConfig,
} from '@choerodon/master';

const Index = (): any => ({
  autoCreate: true,
  fields: [{
    name: 'roleIds',
    multiple: true,
    type: 'object',
    label: '角色',
    textField: 'name',
    required: true,
    valueField: 'id',
    options: new DataSet({
      autoQuery: true,
      transport: {
        read: () => ({
          ...organizationsApiConfig.roleList(),
          transformResponse: (response: any) => {
            let res = response;
            try {
              res = JSON.parse(res);
              return res;
            } catch (e) {
              return res;
            }
          },
        }),
      },
    }),
  }, {
    name: 'userLabels',
    type: 'object',
    multiple: true,
    label: '标签',
    textField: 'name',
    valueField: 'value',
    options: new DataSet({
      autoQuery: true,
      transport: {
        read: () => ({
          ...organizationsApiConfig.userLabelList(),
          transformResponse: (response: any) => {
            let res = response;
            try {
              res = JSON.parse(res);
              return res.content.map((i: any) => ({
                name: i,
                value: i,
              }));
            } catch (e) {
              return res;
            }
          },
        }),
      },
    }),
  }],
});

export default Index;
